﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.Utility;
using YouHP.BLL;

namespace YouHP.Pages.Admin
{
    public class ListTreatments : YouHP.Pages.BasePage
    {
        protected List<TreatmentInfo> TrmList { get; set; }
        protected int PageCount { get; set; }
        protected string PageNumbers { get; set; }
        protected int PageIndex { get; set; }
        protected const int PageSize = 15;

        private int _treatmentCount;
        protected int TrmCount
        {
            get { return _treatmentCount; }
        }

        protected override void ShowPage()
        {
            base.ShowPage();

            if (IsPost) HandlePost();
            else HandleGet();
        }

        public void HandlePost()
        {
            string[] trmitemid = Utils.SplitString(RequestTools.GetFormString("trmitemid"), ",");
            bool succ = true;
            List<int> idlst = new List<int>();
            if (succ)
            {
                if (trmitemid.Length <= 0) succ = false;
                else if (trmitemid.Length > PageSize) succ = false;
            }

            if (succ)
            {
                foreach (string str in trmitemid)
                {
                    int id = Utils.StrToInt(str, -1);
                    if ( id <= 0)
                    {
                        succ = false;
                        break;
                    }
                    idlst.Add(id);
                }
            }


            if (succ)
            {
                TreatmentBLL.Instance.DeleteTreatment(idlst.ToArray());
                this.SetUrl("ListTreatments.aspx");
                this.SetMetaRefresh(2, "ListTreatments.aspx");
                this.AddMsgLine("删除成功");
            }
            else
            {
                this.AddErrLine("删除失败");
            }


        }
        public void HandleGet()
        {
            
            PageIndex = RequestTools.GetQueryInt("page", 1);
            TrmList = TreatmentBLL.Instance.GetTreatmentList(PageSize, PageIndex, out _treatmentCount);
            PageCount = Utils.GetPageCount(TrmCount, PageSize);
            PageNumbers = Utils.GetPageNumbers(PageIndex, PageCount, "ListTreatments.aspx", 8, "page", null);
        }
    }
}
